package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.just.agentweb.AgentWeb;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.BrandBean;
import org.gangcai.mac.shop.bean.CategoryBean;
import org.gangcai.mac.shop.common.BaseActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.manager.RetrofitManagers;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.views.TranspanentTitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class BrandActivity extends BaseActivity {

    @BindView(R.id.titleBar)
    TranspanentTitleBar titleBar;
    @BindView(R.id.brand_rv_list)
    RecyclerView brand_rv_list;
    ArrayList<BrandBean.BrandDBean> datalist = new ArrayList<>();
    private CommonAdapter<BrandBean.BrandDBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brand);
        ButterKnife.bind(this);
        titleBar.getBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        brand_rv_list.setLayoutManager(new LinearLayoutManager(this));
        loadData();
    }

    private void loadData() {
        adapter = new CommonAdapter<BrandBean.BrandDBean>(this, R.layout.brand_recyclerview_item, datalist) {
            @Override
            protected void convert(ViewHolder holder, BrandBean.BrandDBean o, int position) {
                if(o.getBrand_logo().length()>8){
                    holder.setVisible(R.id.brandText,false);
                    holder.setVisible(R.id.brandImg,true);
                    Glide.with(BrandActivity.this).load(Constant.BRAND_LOGO + o.getBrand_logo()).into((ImageView) holder.getView(R.id.brandImg));
                }else{
                    holder.setVisible(R.id.brandText,true);
                    holder.setVisible(R.id.brandImg,false);
                    holder.setText(R.id.brandText, o.getBrand_name());
                }

            }

        };
        brand_rv_list.setLayoutManager(new GridLayoutManager(this, 4));
        brand_rv_list.setAdapter(adapter);
        RetrofitManagers.getInstance().create(AppAPI.class).brand("brand")
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<BrandBean>bindToLifecycle())
                .subscribe(new CommonObserver<BrandBean>() {
                    @Override
                    public void onSuccess(BrandBean value) {
                        if (value.getCode() == 101) {
                            datalist.clear();
                            datalist.addAll(value.getInfo());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Toast.makeText(BrandActivity.this, R.string.network_err, Toast.LENGTH_LONG).show();
                    }
                });
    }
}
